package com.tanhua.server.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.MyPageResultVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;


    /**
     *用户通用设置 - 读取
     * @return
     */
    public SettingsVo settings() {
        //创建vo对象
        SettingsVo settingsVo = new SettingsVo();
        //根据线程存储数据进行赋值
        Long userId = UserHolder.getUserId();
        settingsVo.setId(userId);
        settingsVo.setPhone(UserHolder.getMobile());
        //获取当前用户的通知设置
        Settings settings = settingsApi.findById(userId);
        //如果为空则使用默认,不为空进行赋值
        if (settings != null){
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        //获取当前用户的陌生人问题
        Question question = questionApi.findById(userId);
        String txt = question == null ? "你喜欢巨人么?":question.getTxt();
        settingsVo.setStrangerQuestion(txt);

        return settingsVo;
    }

    /**
     * 设置陌生人问题 - 保存
     * @param txt
     */
    public void question(String txt) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findById(userId);

        if (question == null){
            //如果不存在保存默认
            question = new Question();
            question.setTxt(txt);
            question.setUserId(userId);
            questionApi.save(question);
        }else {
            //如果存在修改陌生人问题
            question.setTxt(txt);
            questionApi.update(question);
        }

    }

    /**
     * 通知设置 - 保存
     * @param map
     */
    public void notificationsSetting(Map map) {
        Long userId = UserHolder.getUserId();

        boolean likeNotification = (boolean) map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");

        Settings settings = settingsApi.findById(userId);

        if(settings == null){
            settings = new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);

            settingsApi.save(settings);
        }else {
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);

            settingsApi.update(settings);
        }
    }

    /**
     * 黑名单分页查询(自己实现思路)
     * @param page
     * @param pagesize
     * @return
     */
    public MyPageResultVo blackList(int page, int pagesize) {
        Long userId = UserHolder.getUserId();

        Page userInfoPage = blackListApi.blackList(userId,page,pagesize);

        MyPageResultVo myPageResultVo = new MyPageResultVo();

        myPageResultVo.setItems(userInfoPage.getRecords());

        myPageResultVo.setCounts(Math.toIntExact(userInfoPage.getTotal()));
        myPageResultVo.setPage(Math.toIntExact(userInfoPage.getCurrent()));
        myPageResultVo.setPagesize(Math.toIntExact(userInfoPage.getSize()));
        myPageResultVo.setPages(Math.toIntExact(userInfoPage.getPages()));

        return myPageResultVo;
    }

    /**
     * 移除黑名单
     * @param uid
     */
    public void removeBlackUser(Long uid) {
        Long userId = UserHolder.getUserId();

        blackListApi.removeBlackUser(uid,userId);
    }

    /**
     * 黑名单分页查询(课本实现思路)
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult blackList2(int page, int pagesize) {
        //1.获取当前用户id
        Long userId = UserHolder.getUserId();

        IPage<UserInfo> iPage = blackListApi.blackList2(page,pagesize,userId);

        PageResult pageResult = new PageResult(page,pagesize,Math.toIntExact(iPage.getTotal()),iPage.getRecords());

        return pageResult;
    }
}
